﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using JM.JCY.BusinessEntity.DSJCYD94;
using JM.JCY.Proxy;
using System.Data;
using System.Linq;

namespace JM.JCY.SharePointComponent.LawArchives.PublicProsecutionDetails
{
    public partial class PublicProsecutionDetailsUserControl : UserControl
    {
        T_AJBSGXDataSet.T_AJBSGXDataTable AJBSGXData;
        T_SJ_AJXXDataSet.T_ALLInfoDataTable AllInfoDT;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string N_BH = Request.QueryString["N_BH"].ToString();
                this.lbBH.Text = N_BH;
                var SJ_AJXXDT = GetAJXXByBH(Convert.ToDecimal(N_BH));
                AllInfoDT = GetXYRInfoByBH(Convert.ToDecimal(N_BH));
                BindData(SJ_AJXXDT);
                AJBSGXData = GetAJBSGXDataByBH(Convert.ToDecimal(N_BH));
                lbBATS.Text = AJBSGXData.Count > 0 ? AJBSGXData[0].N_BATS.ToString() : "0";
                DataRow[] rows = GetXYRByBH(Convert.ToDecimal(N_BH)).Select();
                dlXYR.DataSource = rows;
                dlXYR.DataBind();
                if (rows.Count() > 0)
                {

                    XYRNoRecord.Visible = false;
                }

                else
                    XYRNoRecord.Visible = true;
            }

              
        }

        public T_AJBSGXDataSet.T_AJBSGXDataTable GetAJBSGXDataByBH(decimal BH)
        {
            using (var proxy = new DSJCYD94Proxy())
            {
                return proxy.GetAJBSGXDataByBH(BH);
            }
        }

        public void BindData(T_SJ_AJXXDataSet.T_SJ_AJXXDataTable SJ_AJXXDT)
        {
            var row=SJ_AJXXDT[0];
            this.lbID.Text = string.IsNullOrEmpty(row.C_AH) ? string.Empty : row.C_AH.ToString();
            this.lbPerson.Text = string.IsNullOrEmpty(row.C_CBR) ? string.Empty : row.C_CBR;
            this.lbGetDate.Text = row.IsD_SARQNull()?string.Empty:row.D_SARQ.ToString("yyyy年MM月dd日");
            this.lbPlace.Text = string.IsNullOrEmpty(row.C_YSDW) ? string.Empty : row.C_YSDW;
            this.lbEndDate.Text = row.IsD_JARQNull() ? string.Empty : row.D_JARQ.ToString("yyyy年MM月dd日");
            this.lbFirstInvestigateDate.Text = row.IsD_YSBCRQNull() ? string.Empty : row.D_YSBCRQ.ToString("yyyy年MM月dd日");
            this.lbFirstTransferDate.Text = row.IsD_YSBCCBRQNull() ? string.Empty : row.D_YSBCCBRQ.ToString("yyyy年MM月dd日");
            this.lbSecondInvestigateDate.Text = row.IsD_ESBCRQNull() ? string.Empty : row.D_ESBCRQ.ToString("yyyy年MM月dd日");
            this.lbSecondTransferDate.Text = row.IsD_ESBCCBRQNull() ? string.Empty : row.D_ESBCCBRQ.ToString("yyyy年MM月dd日");

        }
        protected void dlXYR_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            T_SJ_AJXXDataSet.T_SJ_XYRRow row = (T_SJ_AJXXDataSet.T_SJ_XYRRow)e.Item.DataItem;
            Label lbName = (Label)e.Item.FindControl("lbName");
            Label lbGender = (Label)e.Item.FindControl("lbGender");
            Label lbBirth = (Label)e.Item.FindControl("lbBirth");
            Label lbTransferReason = (Label)e.Item.FindControl("lbTransferReason");
            Label lbConclusion = (Label)e.Item.FindControl("lbConclusion");
            Label lbInvestigationReason = (Label)e.Item.FindControl("lbInvestigationReason");
            lbName.Text = string.IsNullOrEmpty(row.C_XM) ? string.Empty : row.C_XM;
            lbGender.Text =row.IsN_XBNull() ? string.Empty :row.N_XB == 1 ? "男" : "女";
            lbBirth.Text = row.IsD_CSRQNull() ? string.Empty : row.D_CSRQ.ToString("yyyy年MM月dd日");
            var YSAY= from all in AllInfoDT
                   where all.N_LX==1 && !string.IsNullOrEmpty(row.C_XM)&&all.C_XM==row.C_XM
                   select all.C_MC;
            lbTransferReason.Text = YSAY.Count()>0?YSAY.First():string.Empty;
            //审查起诉结论
            var SCJLDM = GetDMByBXH(404);
            var SCJL = from dm in SCJLDM
                       where !row.IsN_SJCLQKNull() &&dm.N_DM == row.N_SJCLQK
                       select dm.C_MC;
            lbConclusion.Text = SCJL.Count() > 0 ? SCJL.First() : string.Empty;

             var SJAY= from all in AllInfoDT
                   where all.N_LX==2 && !string.IsNullOrEmpty(row.C_XM)&&all.C_XM==row.C_XM
                   select all.C_MC;
             lbInvestigationReason.Text = SJAY.Count() > 0 ? SJAY.First() : string.Empty;


        }
         //Method to get the data in table T_DM
        public T_DMDataSet.T_DMDataTable GetDMByBXH(decimal BXH)
        {
            using (var proxy = new DSJCYD94Proxy())
            {
                return proxy.GetDMByBXH(BXH);
            }
        }

        public T_SJ_AJXXDataSet.T_SJ_AJXXDataTable GetAJXXByBH(decimal BH)
        {

            using (var proxy = new DSJCYD94Proxy())
            {
                return proxy.GetAJXXByBH(BH);
            }
        }

        public T_SJ_AJXXDataSet.T_ALLInfoDataTable GetXYRInfoByBH(decimal BH)
        {

            using (var proxy = new DSJCYD94Proxy())
            {
                return proxy.GetXYRInfoByBH(BH);
            }
        }

        public T_SJ_AJXXDataSet.T_SJ_XYRDataTable GetXYRByBH(decimal BH)
        {

            using (var proxy = new DSJCYD94Proxy())
            {
                return proxy.GetXYRByBH(BH);
            }
        }
    }
}
